package com.mxx.basic.server.retrofit.observer;

import androidx.appcompat.app.AppCompatActivity;


import com.mxx.basic.server.exception.ApiException;
import com.mxx.basic.utils.Logger;
import com.mxx.basic.utils.ToastUtils;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<T> {
    protected Logger mLogger;
    protected AppCompatActivity mActivity;

    public BaseObserver(AppCompatActivity mActivity) {
        this.mActivity = mActivity;
        mLogger = Logger.getInstance();
    }

    @Override
    public void onSubscribe(@NonNull Disposable d) {
        // 比如显示加载中对话框
        mLogger.i("在向服务器请求数据之前做一些事情：比如显示加载中对话框");
    }

    @Override
    public void onComplete() {
        // 比如隐藏加载中对话框
        if (mLogger != null) {
            mLogger.i("在向服务器请求数据结束做一些事情：比如比如隐藏加载中对话框");
        }
    }


    @Override
    public void onError(Throwable t) {
        if (mLogger != null) {
            mLogger.e(t.getMessage());
            ToastUtils.showToast(mActivity,t.getMessage());
        }
        ApiException.handleException(t);
    }
}
